#include "croot.h"
#include "cdecoration.h"

#include <buola/gui/x11/cx11display.h>
#include <buola/gui/x11/cpixmap.h>
#include <buola/gl.h>
#include "cclient.h"

#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"

namespace buola { namespace bwm {

CDecoration::CDecoration(CClient *pClient)
    :   mClient(pClient)
{
}

CDecoration::~CDecoration()
{
    for(int i=0;i<mDecoWindows.size();i++)
        XDestroyWindow(gui::x11::get_default_x11_display()->GetDisplay(),mDecoWindows[i]);
}

void CDecoration::Create(const CPoint_i &pPos,const CSize_i &pSize,
                                    const CPoint_i &pXPos)
{
    mClientSize=pSize;
    mClientXPos=pXPos;
    InitStatic();
    InitInternal();
}

Window CDecoration::CreateDecoWindow(const CPoint_d &pPos,const CSize_d &pSize)
{
    unsigned long lMask=CWWinGravity|CWEventMask|CWOverrideRedirect;
    XSetWindowAttributes lXAttrs;
    lXAttrs.win_gravity=NorthWestGravity;
    lXAttrs.event_mask=ButtonPressMask|ButtonReleaseMask|PointerMotionMask|
                    EnterWindowMask|LeaveWindowMask;
    lXAttrs.override_redirect=True;

    int lClass=InputOnly;

    Window lID=XCreateWindow(gui::x11::get_default_x11_display()->GetDisplay(),gRoot->GetID(),
                    (int)pPos.x,(int)pPos.y,(int)pSize.x,(int)pSize.y,
                    0,0,lClass,CopyFromParent,lMask,&lXAttrs);
    XMapWindow(gui::x11::get_default_x11_display()->GetDisplay(),lID);
    mDecoWindows.push_back(lID);
    return lID;
}

void CDecoration::Refresh()
{
    eDamage();
}

void CDecoration::OnGeometryChanged(bool pResize,const CPoint_i &pPos,
                            const CSize_i &pSize,const CPoint_i &pXPos)
{
    mClientSize=pSize;
    mClientXPos=pXPos;
}

void CDecoration::OnShapeChanged()
{
}

void CDecoration::OnOpacityChanged()
{
}

void CDecoration::OnClientMapped()
{
    for(int i=0;i<mDecoWindows.size();i++)
        XMapWindow(gui::x11::get_default_x11_display()->GetDisplay(),mDecoWindows[i]);
}

void CDecoration::OnClientUnmapped()
{
    for(int i=0;i<mDecoWindows.size();i++)
        XUnmapWindow(gui::x11::get_default_x11_display()->GetDisplay(),mDecoWindows[i]);
}

void CDecoration::OnDecorationExpose(XExposeEvent *pE)
{
    
}

/*namespace bwm*/ } /*namespace buola*/ }
